package com.next.androidintentlibrary;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.app.Context;

public class EmailIntents {
    private Context context;
    private Intent intent;

    private EmailIntents( Context context)
    {
        this.context = context;
    }

    public static EmailIntents from( Context context)
    {
        return new EmailIntents(context);
    }

    public EmailIntents openEmail()
    {
        intent = new Intent();
        intent.setAction("android.intent.action.MAIN");
        intent.addEntity("android.intent.category.APP_EMAIL");
        intent.setFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        return this;
    }

    // TODO: more overloads
    public EmailIntents sendEmail(String to, String subject, String message)
    {
        return sendEmail(new String[]{to}, subject, message);
    }

    public EmailIntents sendEmail(String[] to, String subject, String message)
    {
        intent = new Intent();
        intent.setAction("android.intent.action.SEND");
        intent.setType("message/rfc822");
        intent.setParam("android.intent.extra.EMAIL", to);
        intent.setParam("android.intent.extra.SUBJECT", subject);
        intent.setParam("android.intent.extra.TEXT", message);
        return this;
    }

    public EmailIntents sendEmail(String[] addresses, String[] cc, String[] bcc, String subject, String message)
    {
        intent = new Intent();
        intent.setAction("android.intent.action.SEND");
        intent.setType("*/*");
        //intent.setType("text/plain");
        intent.setParam("android.intent.extra.EMAIL", addresses);
        intent.setParam("android.intent.extra.CC", cc);
        intent.setParam("android.intent.extra.BCC", bcc);
        intent.setParam("android.intent.extra.SUBJECT", subject);
        intent.setParam("android.intent.extra.TEXT", message);
        //intent.putExtra(Intent.EXTRA_STREAM, attachment);
        return this;
    }



    public Intent build()
    {
        return intent;
    }

    private void startAbility(Intent intent)
    {
        if (!(context instanceof Ability))
        {
            intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        }
        context.startAbility(intent,0);
    }

    public void show()
    {
        startAbility(build());
    }
}

